<?php
require_once ('./Model/Validation.php');
require_once ('./Static/ArrayAssoc.php');

class ArticleEditorView extends Validation{
	private $postHeadlineKey = 'headline';
	private $postArticleKey = 'article';
	private $postSubmitKey = 'submit';
	
	private $getEditKey = 'edit';
	private $getRemoveKey = 'remove';
	
	public static $message = null;
	
	/*
	 * Följande funktioner returnerar alla tänkbara $_POST variabler
	 */
	public function GetHeadline(){
		return (isset($_POST[$this->postHeadlineKey])) ? $this->FormatTextString($_POST[$this->postHeadlineKey], true) : null;
	}
	public function GetArticle(){
		return (isset($_POST[$this->postArticleKey])) ? $this->FormatTextString($_POST[$this->postArticleKey], true) : null;
	}
	public function GetEditId(){
		return (isset($_GET[$this->getEditKey])) ? $_GET[$this->getEditKey] : null;
	}
	public function GetRemoveId(){
		return (isset($_GET[$this->getRemoveKey])) ? $_GET[$this->getRemoveKey] : null;		
	}
	public function TriedToSubmit(){
		return isset($_POST[$this->postSubmitKey]);
	}
	//returnerar ett html formaterar meddelande om meddelande är satt
	private function DoArticleMessageFlash($error){
		return (!is_null(self::$message)) ? ($error === false) ? "<div id='success'>".self::$message."</div>" : "<div id='error'>".self::$message."</div>" : null;
	}
	//returnerar html formulär för artikel
	public function DoArticleSubmitBox($headline = '', $article = '', $error = false){
		return "<div class='wrapper'>
					<form method='POST' action=''>
						<div>
							<h1>Write Article</h1>
							".$this->DoArticleMessageFlash($error)."
							<div>
								<div>
									Headline:
								</div>
								<div>
									<input type='text' size='77' name='$this->postHeadlineKey' maxlength='40' value='$headline' />
								</div>
							</div>
							<div>
								<div>
									Article:
								</div>
								<div>
									<textarea name='$this->postArticleKey' rows='10' cols='65'>$article</textarea>
								</div>
							</div> 
							<div>
								<input type='submit' name='$this->postSubmitKey' value='Submit Article' />
							</div>
						</div>
					</form>
				</div>";		
	}
	//returnerar html lista med artiklar
	public function DoArticleList($articleArray, $error = false){
		$html = '';
		
		foreach ($articleArray as $value) {
			$html .= "<div style='background-color:#fff; border-bottom:1px solid #000;'>
						<div style='float:left'>
							<b>".$value[ArrayAssoc::ARTICLE_HEADLINE]."</b><br />Date: ".$value[ArrayAssoc::ARTICLE_DATE]."
						</div>
						<div style='float:right;'>
							<a href='index.php?".NavView::$getPageKey."=".Action::ARTICLE_EDIT."&".$this->getEditKey."=".$value[ArrayAssoc::ARTICLE_ARTICLEID]."'>Edit</a> 
							<a href='index.php?".NavView::$getPageKey."=".Action::ARTICLE_LIST."&".$this->getRemoveKey."=".$value[ArrayAssoc::ARTICLE_ARTICLEID]."'>Remove</a>
						</div>
						<div class='clear'></div>	
					</div>";	
		}
		return "<div class='wrapper'>
					<h1>Your Articles</h1>
					".$this->DoArticleMessageFlash($error)."
					$html
				</div>";		
	}
}
?>